package com.sy.utils;

import com.alibaba.druid.util.StringUtils;
import com.sun.prism.shader.FillCircle_LinearGradient_REFLECT_AlphaTest_Loader;
import com.sy.constant.AppConstant;
import com.sy.exception.BusinessException;
import org.apache.commons.beanutils.BeanUtils;

import java.util.Map;

import static com.sy.constant.AppConstant.*;

public class CommonUtils {
    private CommonUtils() {

    }

    public static <T> Map<String, String> bean2Map(T t) {
        Map<String, String> map;
        try {
            map = BeanUtils.describe(t);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("参数格式转换异常");
        }
    }

    public static <T> T map2Bean(Map<String, String> map, Class<T> clazz) {
        T t;
        try {
            t = clazz.newInstance();
            BeanUtils.populate(t, map);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("参数格式转换异常");
        }
    }

    public static String getContentType(String fileEx){
        //docx和doc文件
        if(StringUtils.equals(fileEx,FILE_EXTENSION_DOCX) || StringUtils.equals(fileEx,FILE_EXTENSION_DOC)){
            return "application/msword";
        }
        //png文件
        if(StringUtils.equals(fileEx,FILE_EXTENSION_PNG)){
            return "application/x-png";
        }
        //ppt文件
        if(StringUtils.equals(fileEx,FILE_EXTENSION_PPT) || StringUtils.equals(fileEx,FILE_EXTENSION_PPTX)){
            return "application/x-ppt";
        }
        //pdf文件
        if(StringUtils.equals(fileEx,FILE_EXTENSION_PDF)){
            return "application/pdf";
        }
        //jpg和jpe文件
        if(StringUtils.equals(fileEx,FILE_EXTENSION_JPG) || StringUtils.equals(fileEx,FILE_EXTENSION_JPE)){
            return "application/x-jpg";
        }
        //txt文件
        if(StringUtils.equals(fileEx,FILE_EXTENSION_TXT)){
            return "text/plain;charset=UTF-8";
        }
        if(StringUtils.equals(fileEx,FILE_EXTENSION_CLASS)){
            return "java/*";
        }
        if(StringUtils.equals(fileEx,FILE_EXTENSION_CML) || StringUtils.equals(fileEx,FILE_EXTENSION_XML)){
            return "text/xml";
        }
        if(StringUtils.equals(fileEx, FILE_EXTENSION_CSS)){
            return "text/css";
        }
        if(StringUtils.equals(fileEx, FILE_EXTENSION_EXE)){
            return "application/x-msdownload";
        }
        if(StringUtils.equals(fileEx, FILE_EXTENSION_GIF)){
            return "image/gif";
        }
        if(StringUtils.equals(fileEx, FILE_EXTENSION_HTML)){
            return "text/html";
        }
        if(StringUtils.equals(fileEx, FILE_EXTENSION_IMG)){
            return "application/x-img";
        }
        if(StringUtils.equals(fileEx, FILE_EXTENSION_JAVA)){
            return "java/*";
        }
        if(StringUtils.equals(fileEx, FILE_EXTENSION_JS)){
            return "application/x-javascript";
        }

        return null;
    }

}
